package cn.didi.feidi.androidorder.fragment.personal;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.donkingliang.labels.LabelsView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.model.CommentUser;
import cn.didi.feidi.androidorder.model.OrderUser;
import cn.didi.feidi.androidorder.util.AppUtil;

public class PersonJudgeWatchFragment extends FeidiBaseFragment {

    private static final String TAG = "PersonAboutUsFragment";
    private ImageView deliverIcon;
    private TextView deliverName;
    private TextView finishSendTime;
    private SimpleRatingBar deliveryJudge;
    private SimpleRatingBar goodsJudge;
    private LabelsView DeliveryManJudgeLabels;
    private TextView personOrderId;
    private TextView isNoNameJudgeTv;
    private TextView judgeTime;

    private OrderUser orderUser;

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */


    public static PersonJudgeWatchFragment newInstance(Bundle bundle) {
        PersonJudgeWatchFragment fragment = new PersonJudgeWatchFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderUser = (OrderUser) getActivity().getIntent().getSerializableExtra("orderUser");
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_watch_judge_result, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }


    @Override
    protected void initViews(View rootView) {
        deliverIcon = (ImageView) rootView.findViewById(R.id.person_judge_result_deliver_icon);
        deliverName = (TextView) rootView.findViewById(R.id.person_judge_result_deliver_name);
        finishSendTime = (TextView) rootView.findViewById(R.id.person_judge_result_finish_deliver_time);

        deliveryJudge = (SimpleRatingBar) rootView.findViewById(R.id.person_judge_result_delevery_judge);
        goodsJudge = (SimpleRatingBar) rootView.findViewById(R.id.person_judge_result_goods_judge);

        DeliveryManJudgeLabels = (LabelsView) rootView.findViewById(R.id.person_judge_result_delevery_man_judge);

        personOrderId = (TextView) rootView.findViewById(R.id.person_judge_result_order_id);
        isNoNameJudgeTv = (TextView) rootView.findViewById(R.id.person_judge_result_is_no_name_judge);
        judgeTime = (TextView) rootView.findViewById(R.id.person_judge_result_judge_time);
    }

    @Override
    protected void initData() {
        getWaterDeliverIcon();
        getCommonContent();

        deliverName.setText(orderUser.getWaterDeliverName());
        finishSendTime.setText("送达时间 " + AppUtil.formatTimeInMillis(orderUser.getWaDeCoTi()));
        personOrderId.setText(orderUser.getOrderUserId());
    }

    @Override
    protected void addListener() {

    }

    private void getWaterDeliverIcon(){
        Map<String, String> data = new HashMap<String, String>();
        data.put("userId", UserManager.getUserId());
        data.put("token", UserManager.getToken());
        data.put("waterDeliverId", orderUser.getWaterDeliverId());

        executeRequest(new AppActionRequest<>(Api.GET_WATER_DELIVER_ICON_AND_PORTRAIT, data, new TypeToken<ApiResponse<String>>() {
        }.getType(), new ActionCallbackListener<String>() {
            @Override
            public void onSuccess(String message, String data) {

                Glide.with(mActivity).load(data).asBitmap().placeholder(R.drawable.person_center_person_default_icon).centerCrop().into(new BitmapImageViewTarget(deliverIcon) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        deliverIcon.setImageDrawable(circularBitmapDrawable);
                    }
                });

            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
            }
        }).setEmergency(true));
    }

    private void getCommonContent(){
        Map<String, String> data = new HashMap<String, String>();
        data.put("userId", UserManager.getUserId());
        data.put("token", UserManager.getToken());
        data.put("commentUserId", orderUser.getCommentUserId());

        executeRequest(new AppActionRequest<>(Api.GET_ORDER_COMMON_CONTENT, data, new TypeToken<ApiResponse<CommentUser>>() {
        }.getType(), new ActionCallbackListener<CommentUser>() {
            @Override
            public void onSuccess(String message, CommentUser data) {
                deliveryJudge.setRating(data.getDeliverGrade());
                goodsJudge.setRating(data.getProductGrade());

                Gson gson = new Gson();
                DeliveryManJudgeLabels.setLabels((ArrayList<String>) gson.fromJson(data.getContent(),List.class));

                if (data.getAnonymous()){
                    isNoNameJudgeTv.setText("匿名评价");
                } else {
                    isNoNameJudgeTv.setText("非匿名评价");
                }

                judgeTime.setText(AppUtil.formatTimeInMillis(data.getCreateTime()));

            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
            }
        }).setEmergency(true));
    }

}
